import { useUserInfo } from "@/store/index.js";

export const hasPerm = {
  mounted(el, binding) {
    checkPermission(el, binding);
  },
  updated(el, binding) {
    checkPermission(el, binding);
  },
};

const checkPermission = (el, binding) => {
  const { value } = binding;
  const useUserInfoStore = useUserInfo();
  const userPrem = useUserInfoStore.userInfo.permissions;

  // 如果没有传入权限值，则默认显示
  if (!value) {
    return;
  }

  let hasPermission = false;

  // 支持字符串格式
  if (typeof value === "string") {
    hasPermission = userPrem.includes(value);
  }
  // 支持数组格式
  else if (Array.isArray(value)) {
    hasPermission = value.some((item) => userPrem.includes(item));
  }

  el.style.display = hasPermission ? "" : "none";
};

export default hasPerm;
